<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
			<div class="app-space-between">
				<div class="app-space-between-left">
					<el-form-item label="商品类目名称:" prop="goodsCategoryName">
						<el-input v-model="queryParams.goodsCategoryName" placeholder="请输入商品类目名称" clearable
							@clear="handleQuery" @keyup.enter.native="handleQuery" />
					</el-form-item>
				</div>
				<div class="app-space-between-right">
					<el-form-item>
						<el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
						<el-button size="mini" @click="resetQuery">重置</el-button>
					</el-form-item>
				</div>
			</div>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" @click="handleAdd">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange"
			:tree-props="{children: 'children', hasChildren: 'hasChildren'}" row-key="goodsCategoryId"
			:default-expand-all="true">
			<el-table-column show-overflow-tooltip type="selection" width="55" align="center" />
			<el-table-column show-overflow-tooltip label="商品类目名称" width="300" align="center" prop="goodsCategoryName" />
			<el-table-column show-overflow-tooltip label="排序" align="center" prop="sort"></el-table-column>
			<el-table-column show-overflow-tooltip label="创建时间" align="center" prop="creatorTime">
				<template slot-scope="scope">
					{{scope.row.creatorTime | $YYYY_MM_DD_hh_mm_ss}}
				</template>
			</el-table-column>
			<el-table-column show-overflow-tooltip label="操作" align="center" width="140" fixed="right">
				<template slot-scope="scope">
					<el-link type="primary" @click="handleUpdate(scope.row)" :underline="false">编辑</el-link>
					<el-link type="primary" @click="handleDelete(scope.row)" :underline="false" style="margin:0 6px;">删除
					</el-link>
				</template>
			</el-table-column>
		</el-table>
		<!-- 	<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" /> -->

		<!-- 增 改 -->
		<categoryAdd ref="category-add" @success="handleQuery"></categoryAdd>
	</div>
</template>

<script>
	import {
		listWcGoodsCategory,
		deleteWcGoodsCategory,
	} from "@/api/culturalCreativity/category.js"
	import categoryAdd from "./components/category-add.vue";
	export default {
		components: {
			categoryAdd,
		},
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 岗位表格数据
				postList: [],
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 99999,
					goodsCategoryName: ""
				},
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询岗位列表 */
			getList() {
				this.loading = true;
				listWcGoodsCategory({}, this.queryParams).then(response => {
					this.postList = this.handleTree(response.rows, "goodsCategoryId", 'goodsCategoryParentId');
					this.loading = false;
				});
			},

			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.goodsCategoryId)
				this.single = selection.length != 1
				this.multiple = !selection.length;
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.$refs['category-add'].init();
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				let postId = row.goodsCategoryId || this.ids
				if (Array.isArray(postId)) postId = postId.toString();
				this.$refs['category-add'].init(postId);
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				let postIds = row.goodsCategoryId || this.ids;
				if (Array.isArray(postIds)) postIds = postIds.toString()
				this.$modal.confirm('此操作将要删除所选的数据，是否继续？').then(function() {
					return deleteWcGoodsCategory(postIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			}
		}
	};
</script>
